package com.liu.qqRobot.myself.job;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.liu.common.content.RedisKeyInterface;
import com.liu.common.utils.QQClientUtils;
import com.liu.qqRobot.myself.entity.dto.ArbitrationInfoDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.List;

@Slf4j
@Component
public class ArbitrationJob {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Scheduled(cron = "0 0/50 * * * ?")
    public void arbitration() {
        log.info("开始查询仲裁信息");
        String objectStr = redisTemplate.opsForValue().get(RedisKeyInterface.WARFRAME_ARBITRATION_INFO);
        List<ArbitrationInfoDTO> arbitrationInfoDTOS = JSONUtil.toList(objectStr, ArbitrationInfoDTO.class);
        // 需要获取北京时间
        DateTime nowTime = DateUtil.offsetHour(DateUtil.date(), 8);
        // 需要获取北京时间
        ArbitrationInfoDTO arbitrationInfoDTO = arbitrationInfoDTOS.stream().filter(a -> {
                    boolean result = false;
                    DateTime time = DateUtil.parse(a.getTime());
                    if (time.after(nowTime)) {
                        result = true;
                    }
                    return result;
                }).min(Comparator.comparing(a -> DateUtil.parse(a.getTime())))
                .orElse(null);
        if (arbitrationInfoDTO != null) {
            long between = DateUtil.between(nowTime, DateUtil.parse(arbitrationInfoDTO.getTime()), DateUnit.SECOND);
            if (between <= 60 * 15) {
                QQClientUtils.sendMsg("地图：" + arbitrationInfoDTO.getMapName() + "在：" + arbitrationInfoDTO.getTime() + "出现高价值仲裁任务",
                        675080584L, 938354542L, true);
                QQClientUtils.sendMsg("地图：" + arbitrationInfoDTO.getMapName() + "在：" + arbitrationInfoDTO.getTime() + "出现高价值仲裁任务",
                        675080584L, 302418546L, true);
                QQClientUtils.sendMsg("地图：" + arbitrationInfoDTO.getMapName() + "在：" + arbitrationInfoDTO.getTime() + "出现高价值仲裁任务",
                        675080584L, 313681214L, true);
            }
        }
        log.info("结束查询仲裁信息");
    }
}
